package com.lagou.sqlSession;

import lagou.io.pojo.Configuration;
import lagou.io.pojo.MappedStagement;


import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }
    @Override
    public <E> List<E> selectList(String statementid, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
       //将要去完成
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStagement mappedStagement = configuration.getMappedStagement().get(statementid);
        List<E> query = simpleExecutor.query(configuration, mappedStagement, params);

        return query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        List<Object> objects = selectList(statementId, params);
        if(objects.size()==1){
            return (T) objects.get(0);
        }else{
            throw new RuntimeException("查询结果为空或者返回结果过多不唯一");
        }
    }

    @Override
    public int update(String statementId, Object... params) throws Exception {
        //将要去完成
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStagement mappedStagement = configuration.getMappedStagement().get(statementId);
        int i =  simpleExecutor.update(configuration, mappedStagement, params);
        return i;
    }

    @Override
    public <T> T getMap(Class<?> mapperClass) {

        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method,Object[] args) throws Throwable {
                //底层JDBC,
                //获取参数nameSpace+id :将类名当做nameSapce, id 设置为方法名称。
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className+"."+methodName;
                //准备参数2：params:args;
                //获取被调用方法的返回类型
                Type genericReturnType = method.getGenericReturnType();
                //判断返回类型是否是泛型
               int i = update(statementId,args);
              /*  if(genericReturnType instanceof  ParameterizedType){
                    List<Object>  objects= selectList(statementId, args);
                    return objects;

                }
                else {
                    Object object = selectOne(statementId, args);
                    return object;
                }*/
            return i;

            }
        });
        return (T) proxyInstance;
    }
}
